<?php
header('Content-Type: application/json');
Header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Origin:*');   //跨域

require './mysql_init.php';

//检查数据是否正常
function limit_up_data($session_id){
    $flag=1;
    if(gettype($session_id) != 'string'){
        $flag=0;
    }
    if(strlen($session_id) < 2 || strlen($session_id) > 300){
        $flag=0;
    }
    return $flag;
}
//获取邮箱
function get_email($pdo,$session_id){
    try {
        // 准备查询语句
        $query = "SELECT email FROM session WHERE session_id = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $session_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['email'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//获取uid
function get_uid($pdo,$email){
    try {
        // 准备查询语句
        $query = "SELECT uid FROM users WHERE email = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $email);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['uid'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//判断该用户是否有此地址
function limit_is_user_address($pdo,$uid,$address_id){
    try {
        // 准备查询语句
        $query = "SELECT count(*) FROM shipping_address WHERE user_uid=? and address_id=?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $uid);
        $statement->bindParam(2, $address_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['count(*)'];      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }

}
//修改地址
function change_address($pdo,$uid,$address_id){
    try {
        // 准备修改语句
        $query = "UPDATE shipping_address SET is_forbidden=1 WHERE address_id=? and user_uid=?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $address_id);
        $statement->bindParam(2, $uid);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->rowCount();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $json=file_get_contents('php://input');  //获取post请求中的原始数据
    $post_json=json_decode($json,true);       //把原始数据转化成json

    $session_id=$post_json['session_id'];
    $address_id=$post_json['address_id'];
    
    $response = array(
        'status' => 0,
        'message' =>'初始化'
    );
    if(limit_up_data($session_id)==1){      //验证上传的session_id数据类型是否合法
        if(gettype(get_email($pdo,$session_id))=='string'){
            $email=get_email($pdo,$session_id);
            $uid=get_uid($pdo,$email);

            if(limit_is_user_address($pdo,$uid,$address_id) == 1){
                $n=change_address($pdo,$uid,$address_id);
                $response = array(
                    'status' => 1,
                    'message' => '删除成功',
                    'data'=>$n
                );
            }else{
                $response = array(
                    'status' => 0,
                    'message' => '修改别人的私密数据是违法行为，立即停止'
                );
            }
        }else{
            $response = array(
                'status' => 0,
                'message' => '用户不存在'
            );
        }
    }else{
        $response = array(
            'status' => 0,
            'message' => '数据类型错误'
        );
    }

    echo json_encode($response);
} else{             //如果不是post请求
    $response = array(
        'status' => 0,
        'message' => '错误请求'
    );
    echo json_encode($response);
}
?>